# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 -------------------------------------------------
    File Name:     result_model.py
    Description:   结果模型
 -------------------------------------------------
 """

from datetime import datetime
from typing import List


class TestCaseResult:
    """单个测试用例结果"""

    def __init__(self, case_id: str = None, name="调试运行"):
        self.case_id = case_id  # 唯一标识符
        self.name = name  # 用例名称
        self.status = "UNKNOWN"  # PENDING/Passed/Failed/Skipped
        self.start_time = None  # 开始时间
        self.end_time = None  # 结束时间
        self.duration = 0  # 持续时间(秒)
        self.error_trace = ""  # 错误堆栈
        self.assertions = []  # 断言详情列表

    def finish(self, status: str, error: str = None):
        self.status = status
        self.end_time = datetime.now()
        self.duration = (self.end_time - self.start_time).total_seconds()
        if error:
            self.error_trace = error

    @property
    def elapsed(self) -> float:
        return (self.end_time - self.start_time).total_seconds() if self.end_time else 0


class TestSuiteResult:
    """
    测试套件聚合结果
    """

    def __init__(self, name: str):
        self.name = name
        self.test_cases = []  # TestCaseResult列表
        self.total = 0  # 总用例数
        self.passed = 0  # 通过数
        self.failed = 0  # 失败数
        self.skipped = 0  # 跳过数
        self.start_time = None  # 套件开始时间
        self.end_time = None  # 套件结束时间
        self.duration = 0  # 总耗时

    def add_case(self, case_result: TestCaseResult):
        self.test_cases.append(case_result)
        self.total += 1
        if case_result.status == "Passed":
            self.passed += 1
        elif case_result.status == "Failed":
            self.failed += 1
        elif case_result.status == "Skipped":
            self.skipped += 1

    def finish(self):
        self.end_time = datetime.now()
        self.duration = (self.end_time - self.start_time).total_seconds()

    @property
    def elapsed(self) -> float:
        return (self.end_time - self.start_time).total_seconds() if self.end_time else 0
